var js_config = {};
layui.use(['form'], function () {
    var form = layui.form
        , layer = layui.layer;

    /**
     * 页面初始化，数据渲染
     */
    function initPage() {
        var token = sessionStorage.access_token;
        if(token==null){
            againToken();
            return;
        }
        var vue = new Vue({
            el: '#index_div',
            data: {
                user_info:{},
                data_list: {},
                next_page: 1,//下一页的页码
                allpage: 1,//总页码，会从后台获取
                type: 0,   //0 未挑战 1挑战中
                page_size: 12, //每页条数
            },
            watch: {},
            methods: {
                switchType: function (val) {
                    if (this.type == val) {
                        return;
                    }
                    this.type = val;
                    this.next_page = 1;
                    initDataList();
                }
            },
            filters: {
                integral: function (value) {
                    return parseInt(value);
                },
                getAnswerHref: function (val) {
                    return 'answer.html?id=' + val;
                }
            }
        });

        /**
         * 滚动事件
         */
        function scrollFn() {
            //真实内容的高度
            var pageHeight = Math.max(document.body.scrollHeight, document.body.offsetHeight);
            //视窗的高度
            var viewportHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0;
            //隐藏的高度
            var scrollHeight = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
            if (pageHeight - viewportHeight - scrollHeight < 20) {  //如果满足触发条件，执行
                if (vue.next_page <= vue.allpage) {
                    initDataList();
                }
            }
        }

        $('.tab-show-box').bind("scroll", scrollFn);    //绑定滚动事件

        /**
         * 初始化页面数据
         */
        function initDataList() {

            var data = {
                token: token,
                page_size: vue.page_size,
                page_num: vue.next_page,
                status: vue.type,
            };

            //获取挑战列表信息
            $.ajax({
                type: "post"
                , url: '/index/challenge/ajax_challenge_list'
                , data: data
                , async: false
                , success: function (result) {
                    if (result.code == 0) {
                        againToken();
                        return;
                    }
                    if (result.code == 1) {
                        if (result.data.page.pagenum == "1") {
                            vue.data_list = result.data.list;
                        } else {
                            vue.data_list.push.apply(vue.data_list, result.data.list);
                        }
                        vue.next_page = parseInt(result.data.page.pagenum) + 1;
                        vue.allpage = result.data.page.totalpage;
                    }
                }
                , error: function (resq) {
                    console.info(resq);
                    layer.msg("服务器异常");
                }
            });
        }

        /**
         * 绑定上级用户
         */
        function bindUser() {
            var uid = getQueryString('uid');
            if (uid != null) {  //判断是否存在uid
                console.log(uid);
                var data = {
                    token: token,
                    user_id: uid
                }
                $.post('/index/user/ajax_bind_user', data, function (result) {
                    console.info(result);
                    if (result.code == 0) {
                        againToken();
                    }
                })
            }
        }

        /**
         * 初始化JS-SDK配置信息
         */
        function initJsConfig() {

            var data = {
                token: token,
                url:window.location.href
            }
            $.post("/index/config/ajax_get_wechat_js", data, function (result) {
                //if (result.code == -1) {   //失败
                //    layer.msg(result.msg);
                //    return;
                //}
                if (result.code == 0) { //Token失效
                    againToken();
                    return;
                }
                if (result.code == 1) { //成功
                    vue.js_config = result.data;
                    js_config = vue.js_config;
                    startJsSDK();
                }

            });
        }

        initDataList();
        bindUser();
        initUserInfo(vue,token);
        initJsConfig();
    };

    /**
     * 切换初始化
     */
    function initIndexTabContent() {
        //切换显示内容
        var headH = $('.main-index-header').height();
        var tabH = $('.tab-box').height();
        var tabShowBoxH = winH - headH - tabH - 49 - 44 - 20;
        $('.tab-show-box').height(tabShowBoxH + 'px');
    };

    /**
     * 初始化完成加载函数
     */
    $(function () {
        initGlobalPage();
        initIndexTabContent();
        initPage();
    });

});